package v2;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.PriorityScheduler;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;
import vo.PinPaiVO;

public class SnygPinPaiUrlListPageProcessor implements PageProcessor{
	private Site site = Site.me()
			.setUserAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36")
			.setSleepTime(1000).setRetryTimes(2).setCycleRetryTimes(1000);
	
	private Map<String,PinPaiVO> pinPaiMap;
	
	public Map<String, PinPaiVO> getPinPaiMap() {
		String url = "http://list.suning.com/0-20006-0.html";
		long startTime = new Date().getTime();
		Spider spider = Spider.create(this).scheduler(new PriorityScheduler());
		spider.addUrl(url);
		spider.thread(3).run();
		
		long endTime = new Date().getTime();
		System.out.println("时间："+String.valueOf((endTime-startTime)));
		return this.pinPaiMap;
	}


	public void setPinPaiMap(Map<String, PinPaiVO> pinPaiMap) {
		this.pinPaiMap = pinPaiMap;
	}


	@Override
	public void process(Page page) {
		String url = page.getUrl().get();
		if(url.contains("list.suning.com")){
			//添加所有手机品牌的入口url
			getPinPaiUrls(page);
		}
	}
	
	private void getPinPaiUrls(Page page){
		this.pinPaiMap = new HashMap<>();
		
		
		List<Selectable> selectables = page.getHtml().xpath("//div[@class='brand-item brand-con']//div[@class='clearfix']//a").nodes();
		
		//获取展开的品牌信息
		String extString = page.getHtml().xpath("//div[@class='brand-item brand-con']//textarea/allText()").toString();
		extString = extString.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
		Page page2 = new Page();
		Html html = new Html(extString);
		page2.setHtml(html);
		List<Selectable> extSelectables = page2.getHtml().xpath("//a").nodes();
		if(extSelectables != null && extSelectables.size()>0){
			selectables.addAll(extSelectables);
		}else{
			System.out.println("获取展开的品牌的逻辑有误！");
		}
		if(selectables != null && selectables.size()>0){
			int i=0;
			for(Selectable selectable : selectables){
				String id = ""+i;
				String name = selectable.xpath("//a/@title").toString();
				String url = selectable.xpath("//a/@href").toString();
				
				if(name != null && url != null){
					PinPaiVO jdPinPaiVO = new PinPaiVO();
					jdPinPaiVO.setId(id);
					jdPinPaiVO.setName(name);
					jdPinPaiVO.setUrl(url);
					this.pinPaiMap.put(id, jdPinPaiVO);
				}
				
				i++;
			}
		}
	}
	
	@Override
	public Site getSite() {
		return site;
	}
	
	public static void main(String[] args) {
		Map<String, PinPaiVO> pinpais = new SnygPinPaiUrlListPageProcessor().getPinPaiMap();
		for(Map.Entry<String, PinPaiVO> entry : pinpais.entrySet()){
			System.out.println(entry.getValue().getName());
		}
	}
}
